package org.hismig.test.migration;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import junit.framework.TestCase;

import org.hismig.cfg.Configuration;
import org.hismig.cfg.Param;
import org.hismig.cfg.Trigger;
import org.hismig.migration.Constructor;
import org.hismig.report.JobResultReport;
import org.hismig.scheduler.ITrigger;
import org.hismig.view.MainFrame;

/**
 * @declare: //TODO <br />
 *           This class created by Vansion.Hopihit on 2012-8-10 <br />
 *           <a href="mailto:vansionhopihit@gmail.com">Contact Me</a>
 * @version 1.0
 * @author Vansion.Hopihit
 * 
 */
public class ViewTester extends TestCase {

	private Constructor constructor;
	private Configuration configuration;

	@Override
	protected void setUp() throws Exception {
		configuration = new Configuration(ViewTester.class.getResource(
				"HisMig.cfg.xml").toURI());
		constructor = new Constructor();
		constructor.init(configuration);
	}

	public void testView() {

		ITrigger trigger = constructor.getTrigger();

		Trigger triggerCfg = configuration.getTrigger();
		List<Param> params = triggerCfg.getParams();
		for (Param param : params) {

			if (param.getName().equals("expression")) {

				Calendar calendar = Calendar.getInstance();
				calendar.add(Calendar.SECOND, 10);

				String second = new SimpleDateFormat("ss").format(calendar
						.getTime());

				param.setValue(String.format("%s * * * * ? *",
						new Object[]{second}));
			}

		}

		MainFrame testJFrame = new MainFrame(null);

		testJFrame.start();
		trigger.addListener(testJFrame);
		trigger.addListener(new JobResultReport());

		trigger.execute(configuration);

		try {
			Thread.sleep(120 * 1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}

}
